package com.openlogic.flowable.config;

import javax.sql.DataSource;

import org.flowable.engine.DynamicBpmnService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

@Configuration
@Component(index = 20)
@Condition(onBean=DataSource.class)
public class FlowableConfig {

  @Bean
  public ProcessEngine createEngine(@Inject("master") DataSource dataSource) {
      ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
      cfg.setActivityFontName("宋体");
      cfg.setLabelFontName("宋体");
      cfg.setAnnotationFontName("宋体");
      cfg.setDataSource(dataSource);
      // 在构建过程引擎时，执行检查并在必要时执行模式的更新. 如果没有表则创建，
      cfg.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
      return cfg.buildProcessEngine();
  }

  @Bean
  public RepositoryService createRepositoryService(@Inject ProcessEngine engine) {
      return engine.getRepositoryService();
  }

  @Bean
  public RuntimeService createRuntimeService(@Inject ProcessEngine engine) {
      return engine.getRuntimeService();
  }

  @Bean
  public TaskService createTaskService(@Inject ProcessEngine engine) {
      return engine.getTaskService();
  }

  @Bean
  public HistoryService createHistoryService(@Inject ProcessEngine engine) {
      return engine.getHistoryService();
  }

  @Bean
  public ManagementService createManagementService(@Inject ProcessEngine engine) {
      return engine.getManagementService();
  }
  
  @Bean
  public IdentityService createIdentityService(@Inject ProcessEngine engine) {
	  return engine.getIdentityService();
  }
  
  @Bean
  public DynamicBpmnService createDynamicbpmnService(@Inject ProcessEngine engine) {
	  return engine.getDynamicBpmnService();
  }
}
